package cn.yzking.studentmanagement;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;

import com.google.android.material.color.DynamicColors;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.textfield.TextInputEditText;

import java.util.Map;

import cn.yzking.studentmanagement.databinding.ActivityMainBinding;
import cn.yzking.studentmanagement.utils.MyDBHelper;
import cn.yzking.studentmanagement.utils.SPSaveAccount;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // 启动动态主题
        DynamicColors.applyToActivitiesIfAvailable(this.getApplication());

        TextInputEditText usernameEditText = binding.etUsername;
        TextInputEditText passwordEditText = binding.etPassword;
        CheckBox rememberCheckBox = binding.cbRemember;
        Button loginButton = binding.buttonLogin;
        Button registerButton = binding.buttonRegister;

        Map<String, String> userInfo = SPSaveAccount.getUserInfo(this);
        if (!userInfo.isEmpty()) {
            usernameEditText.setText(userInfo.get("username"));
            passwordEditText.setText(userInfo.get("password"));
            rememberCheckBox.setChecked(true);
        }

        loginButton.setOnClickListener(v -> {
            String username = usernameEditText.getText().toString().trim();
            String password = passwordEditText.getText().toString();
            MyDBHelper helper = new MyDBHelper(this);

            if (helper.userLogin(username, password)) {
                if (rememberCheckBox.isChecked()) {
                    SPSaveAccount.saveUserInfo(this, username, password);
                } else {
                    SPSaveAccount.removeUserInfo(this);
                }
                new MaterialAlertDialogBuilder(this)
                        .setIcon(R.drawable.ic_error)
                        .setTitle("登陆成功")
                        .setMessage("登陆成功!")
                        .setPositiveButton("确定", null)
                        .setOnDismissListener(dialog -> {
                            Intent intent = new Intent(this, ManagementActivity.class);
                            intent.putExtra("username", username);
                            startActivity(intent);
                        })
                        .show();
            } else {
                new MaterialAlertDialogBuilder(this)
                        .setIcon(R.drawable.ic_error)
                        .setTitle("登陆失败")
                        .setMessage("用户名或者密码错误,请重新输入!")
                        .setNegativeButton("确定", null)
                        .show();
            }
        });

        registerButton.setOnClickListener(v -> {
            Intent intent = new Intent(this, RegisterActivity.class);
            startActivity(intent);
        });
    }
}